﻿using System;
using CoreVirtuoso.Helpers;
using VDS.RDF.Storage;
using VirtuosoDal.Repositories.RdfRepositories.Interfaces.VirtuosoCore;

namespace VirtuosoDal.Repositories.RdfRepositories.VirtuosoCore
{
   public class RuleSetsRepository : IRuleSetsRepository
   {
      public void AddGraphToRuleSet(string ruleSetName, string graphUri)
      {
         this.ModifyRuleSet(ruleSetName, graphUri, false);
      }

      public void RemoveGraphFromRuleSet(string ruleSetName, string graphUri)
      {
         this.ModifyRuleSet(ruleSetName, graphUri, true);
      }

      private void ModifyRuleSet(string ruleSetName, string graphUri, bool remove)
      {
         using (var virtuosoManager = new VirtuosoManager(ConnectionStringsHelper.GetVirtuosoConnectionString()))
         {
            virtuosoManager.Open(false);
            virtuosoManager.ExecuteNonQuery(String.Format("rdfs_rule_set ('{0}', '{1}', {2})",
                                                          ruleSetName,
                                                          graphUri,
                                                          remove ? 1 : 0));
            virtuosoManager.Close(false);
         }
      }
   }
}